home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / FCOPY31.ARJ / COPYFILE.ASM next >
Assembly Source File  |  1992-05-12  |  9KB  |  146 lines

  1. ;-----------------------------------------------------------------------;
  2. ; COPYFILE.ASM                                                          ;
  3. ;                                                                       ;
  4. ; This module contains a C-callable low level function that copies      ;
  5. ; one file to another.  _copyfile is called by the higher level         ;
  6. ; function fcopy and performs the actual file reads and writes.         ;
  7. ;                                                                       ;
  8. ; Parameters passed include file handles for both the source and        ;
  9. ; target files, as well as a FAR pointer to (and size of) a buffer.     ;
  10. ; The _copyfile function thus assumes that both source and target       ;
  11. ; file have been opened and that a FAR buffer has been allocated by     ;
  12. ; farmalloc().  THE POINTER TO THE BUFFER *MUST* BE A FAR POINTER!      ;
  13. ;                                                                       ;
  14. ; Note that this function does NOT close the files after the copy       ;
  15. ; operation.  It is up to the caller to close the files and free        ;
  16. ; the memory, just as the caller opened the files and allocated the     ;
  17. ; memory prior to calling this function.                                ;
  18. ;-----------------------------------------------------------------------;
  19. ; Usage:                                                                ;
  20. ;                                                                       ;
  21. ; #include "fcopy.h"                                                    ;
  22. ;                                                                       ;
  23. ; Function prototype:                                                   ;
  24. ;                                                                       ;
  25. ; int _copyfile (int source, int target, char far *buf, unsigned bsize) ;
  26. ;                                                                       ;
  27. ; Returns:  If successful, returns 0.                                   ;
  28. ;                                                                       ;
  29. ;           If copy fails, returns -1 and sets errno                    ;
  30. ;           and _doserrno to one of the following:                      ;
  31. ;                                                                       ;
  32. ;               EACCES  Permission denied (5)                           ;
  33. ;               EBADF   Bad file number (6)                             ;
  34. ;               DISKFUL Target disk full (-2)                           ;
  35. ;-----------------------------------------------------------------------;
  36. ; Revision history:                                                     ;
  37. ;                                                                       ;
  38. ;       1.0     16 MAR 92       Original.  Written to match             ;
  39. ;                               version 2.0 of fcopy().                 ;
  40. ;                                                                       ;
  41. ;                               Uses conditional assembly for all       ;
  42. ;                               memory models by defining _model.       ;
  43. ;                               Defaults to the small model if          ;
  44. ;                               _model is not defined.                  ;
  45. ;                                                                       ;
  46. ;       1.1     19 APR 92       Uses BP as a scratch register for the   ;
  47. ;                               buffer size, after retrieving stack     ;
  48. ;                               variables.  This speeds up the copy     ;
  49. ;                               loop by eliminating a memory access     ;
  50. ;                               inside the loop.  Also defined text     ;
  51. ;                               equate DISKFUL = -2 as an error code.   ;
  52. ;                               A similar #define was added to fcopy.h. ;
  53. ;                                                                       ;
  54. ;                               The jumps in the error handling code    ;
  55. ;                               were also rearranged so that only one   ;
  56. ;                               error exit routine is now needed.       ;
  57. ;                                                                       ;
  58. ;       1.2     12 MAY 92       Now makes no assumption about setting   ;
  59. ;                               of DS on entry.  For large data models  ;
  60. ;                               (compact and large), sets DS to the     ;
  61. ;                               default data segment.  For huge model,  ;
  62. ;                               sets DS to segment containing errno     ;
  63. ;                               so that errno and _doserrno can be set  ;
  64. ;                               on error.                               ;
  65. ;-----------------------------------------------------------------------;
  66. ;   Copyright (c) 1992 Ray Waters                                       ;
  67. ;   All Rights Reserved                                                 ;
  68. ;-----------------------------------------------------------------------;
  69.  
  70. EACCES  EQU     5                       ; equate for permission denied
  71. EBADF   EQU     6                       ; equate for bad file number
  72. DISKFUL EQU     -2                      ; equate for disk full condition
  73.  
  74. IFDEF   _model                          ; if a _model was
  75.         .MODEL  _model, C               ;  defined, use it
  76. ELSE                                    ; else, default to
  77.         .MODEL  SMALL, C                ;  SMALL model
  78. ENDIF
  79.  
  80. IFDEF   ??version                       ; if using TASM,
  81.         LOCALS                          ;  enable local labels
  82. ENDIF
  83.  
  84.         EXTRN   C errno:WORD            ; global error variable
  85.         EXTRN   C _doserrno:WORD        ; global DOS error variable
  86.  
  87.         .CODE                           ; open code segment
  88.  
  89.         PUBLIC  _copyfile               ; make visible to Linker
  90.  
  91. _copyfile       PROC    C USES si di, \
  92.                 source:WORD, target:WORD, buf:FAR PTR BYTE, bsize:WORD
  93.  
  94.         push    ds                      ; save original data segment
  95.         lds     dx,[buf]                ; load far pointer to buffer
  96.         mov     si,[source]             ; use SI for source handle
  97.         mov     di,[target]             ; use DI for target handle
  98.         mov     bp,[bsize]              ; use BP for buffer size - BP is
  99.                                         ;  no longer needed since we have
  100.                                         ;  retrieved our stack variables
  101.  
  102. @@looptop:                              ; this is the copy loop
  103.         mov     cx,bp                   ; try to read a buffer full
  104.         mov     bx,si                   ; source file handle
  105.         mov     ah,3Fh                  ; DOS read file function
  106.         int     21h
  107.         jc      @@copy_error            ; jump on error
  108.         or      ax,ax                   ; were any bytes left to read?
  109.         jz      @@done                  ; if no, finished (end-of-file)
  110.  
  111.         mov     cx,ax                   ; CX = number of bytes to write
  112.         mov     bx,di                   ; target file handle
  113.         mov     ah,40h                  ; DOS write file function
  114.         int     21h
  115.         jc      @@copy_error            ; jump on error
  116.         cmp     ax,cx                   ; did all bytes get copied?
  117.         je      @@looptop               ; if yes, loop until finished
  118.         mov     ax,DISKFUL              ; if not, set error code
  119.                                         ;  and fall through to error exit
  120.  
  121. @@copy_error:
  122. IF @DataSize EQ 2                       ; if huge data model,
  123.         mov     bx,SEG errno            ;  get segment address
  124.         mov     ds,bx                   ;  of errno into DS
  125. ELSEIF @DataSize EQ 1                   ; if large data model,
  126.         mov     bx,@data                ;  get default data segment
  127.         mov     ds,bx                   ;  into DS
  128. ELSE                                    ; if small data model,
  129.         pop     ds                      ;  restore original data segment
  130. ENDIF
  131.         mov     [errno],ax              ; set errno
  132.         mov     [_doserrno],ax          ; set _doserrno
  133. IF @DataSize                            ; if large data model,
  134.         pop     ds                      ;  restore original data segment
  135. ENDIF
  136.         mov     ax,-1                   ; return int value of -1
  137.         ret                             ; return to caller
  138.  
  139. @@done:
  140.         pop     ds                      ; restore original data segment
  141.         ret                             ; return to caller (AX = 0)
  142.  
  143. _copyfile       ENDP
  144.  
  145.         END
  146.